package com.hpeng.tml.action;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.sys.model.Department;
import com.hpeng.sys.service.Function;
import com.hpeng.tml.service.DepartmentService;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionSupport;

@Component("departmentAction")
@Scope("prototype")
public class DepartmentAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Department department;
	@Resource
	private Function function;
	private List<String> comefromList;
	@Resource
	private DepartmentService deptService;
	private List<Department> departments;
	private String depparentname;
	private String msg;
	private String FN;
	// 分页
	private int pageNumber = 1; // 当前页数
	private Pagination<Department> pagination;
	public String pageLink;

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public String getFN() {
		return FN;
	}

	public void setFN(String fN) {
		FN = fN;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public String getDepparentname() {
		return depparentname;
	}

	public void setDepparentname(String depparentname) {
		this.depparentname = depparentname;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public Pagination<Department> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Department> pagination) {
		this.pagination = pagination;
	}

	public String searchAllDepartment() {
		comefromList = function.getAssetComefrom();
		departments = deptService.findAllDepartment();
		System.out.println(comefromList.size());
		return "success";
	}

	public List<String> getComefromList() {
		return comefromList;
	}

	public void setComefromList(List<String> comefromList) {
		this.comefromList = comefromList;
	}

	public String addNewDepartment() {
		try {
			Long seqLong = function.getMaxSeq("department");
			BigDecimal seq = new BigDecimal(seqLong);
			department.setSeq(seq);
			department.setDepid(function.getDepIdByParentId(department
					.getDepparentid()));
			deptService.save(department);
		} catch (Exception rx) {
			msg = getText("save.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		return searchDepartment();
	}

	public String updateDepartment() {
		try {
			deptService.update(department);
		} catch (Exception rx) {
			msg = getText("update.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("update.success");
		}
		return searchDepartment();
	}

	public String deleteChoosedDepartment() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String depidAll = request.getParameter("depidAll");
			if (!"".equals(depidAll)) {
				String[] dids = depidAll.split(",");
				for (String id : dids) {
					if (function.getSonNumber("department", "depparentid", id) == 0) {
						deptService.delete(id);
					} else {
						msg = getText("该节点有子节点不能删除");
					}
				}
			}
		} catch (Exception rx) {
			msg = getText("del.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("del.success");
		}
		return searchDepartment();
	}

	public String selectDepartmentById() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String depid = request.getParameter("depid");
		department = deptService.findDepartmentById(depid);
		return SUCCESS;
	}

	// 分页显示部门信息
	public String searchDepartment() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String[] hql = { " from Department" };
		String order = " order by depid asc";
		String pageNumberStr = request.getParameter("page");
		if (pageNumberStr != null) {
			pageNumber = Integer.parseInt(pageNumberStr);
		}
		setPagination(deptService.getPagination(pageNumber, hql, null, order));
		pageLink = pagination.getPageLink("searchDepartment.action");
		return "success";
	}

	// 点击树上的节点显示该节点的信息
	public String selectDepartmentInTree() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String depid = request.getParameter("depid");
		int pageNumber = 1;
		if (request.getParameter("page") != null) {
			int pageNumber2 = Integer.parseInt(request.getParameter("page"));
			pageNumber = pageNumber2;
		}
		String[] hql = { " from Department where depid like ?||'%' order by depid asc" };
		String order = " ";
		setPagination(deptService.getPagination(pageNumber, hql,
				new String[] { depid }, order));
		pageLink = pagination
				.getPageLink("selectDepartmentInTree.action?depid=" + depid);
		return "success";
	}

	// 条件查询得出的结果分页
	public String searchDepartmentByPageNumberAndValue() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumberStr = request.getParameter("page");
		int pageNumberInt = Integer.parseInt(pageNumberStr);
		String property = request.getParameter("property");
		String value = request.getParameter("value");
		try {
			String valueUTF_8 = new String(value.getBytes("iso8859-1"), "UTF-8");
			String[] hql = { "from Department where " + property
					+ " like '%'||?||'%' " };
			String order = " order by depid asc";
			setPagination(deptService.getPagination(pageNumberInt, hql,
					new String[] { valueUTF_8 }, order));
			pageLink = pagination
					.getPageLink("searchDepartmentByPageNumberAndValue.action?property="
							+ property + "&value=" + valueUTF_8);
		} catch (UnsupportedEncodingException e) {

		}
		return SUCCESS;
	}

}
